//
//  ActivityViewController.m
//  B段项目
//
//  Created by NeyMAr on 15/9/23.
//  Copyright (c) 2015年 昶博. All rights reserved.
//

#import "ActivityViewController.h"
#import "ActivityCell.h"
#import "ActivityModel.h"
#import "ActivityNextViewController.h"
#import <MJRefresh.h>
#import "AFNetWorkingW.h"
@interface ActivityViewController () <UITableViewDelegate, UITableViewDataSource>

@end

@implementation ActivityViewController

- (void)viewWillAppear:(BOOL)animated
{
    SelfTabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.view.backgroundColor = [UIColor colorWithRed:195 / 255.0 green:222 / 255.0 blue:219 / 255.0 alpha:1];
    self.navigationItem.title = @"麦奇自驾游";
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.067 green:0.694 blue:0.651 alpha:1.000];
    self.navigationController.navigationBar.titleTextAttributes = @{@"Font":[UIFont fontWithName:nil size:22], @"TextColor":[UIColor blackColor]};
  
    
    [self tableViewSubview];
    [self dataHandel];
}


/*刷新     (活动界面)*************/
- (void)loadNewData
{
    [self.tableView.header beginRefreshing];
    self.arr = [NSMutableArray array];
    
    [AFNetWorkingW netWorkingHanderGetGataWithUrl:@"http://a.xcar.com.cn/interface/6.0/getEventList.php" resultBlock:^(id result) {
        for (NSMutableDictionary *dic in result) {
            
            ActivityModel *model = [[ActivityModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arr addObject:model];
        }
        [self.tableView reloadData];
    }];
    
    [self.tableView.header endRefreshing];
}


- (void)tableViewSubview
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375 * w, 667 * h) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = 370 * h;
    
/*活动界面的刷新   ****/
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
}


- (void)dataHandel
{
  /* 菊花出现 */
    self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 32 * w , 32 * h)];
    self.activityIndicator.backgroundColor = [UIColor blackColor];
    _activityIndicator.center = CGPointMake(w * 375 / 2.0, h * 667 / 2.0 - h * 50);
    [_activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [self.view addSubview:_activityIndicator];
    [_activityIndicator startAnimating];
    
    self.arr = [NSMutableArray array];
    [AFNetWorkingW netWorkingHanderGetGataWithUrl:@"http://a.xcar.com.cn/interface/6.0/getEventList.php" resultBlock:^(id result) {
        for (NSMutableDictionary *dic in result) {
            
            ActivityModel *model = [[ActivityModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arr addObject:model];
        }
        [self.tableView reloadData];
        /*菊花消失*/
        [_activityIndicator stopAnimating];
    }];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.arr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (self.arr.count != 0) {
        if (cell == nil) {
            
            cell = [[ActivityCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
        }
        ActivityModel *model = [[ActivityModel alloc]init];
        model = [self.arr objectAtIndex:indexPath.section];
        cell.model = model;
    }
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 7;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActivityNextViewController *next = [[ActivityNextViewController alloc]init];
    
    ActivityModel *model = [[ActivityModel alloc]init];
    model = [self.arr objectAtIndex:indexPath.section];
    
    /* 属性传值 */
    next.strUrl = model.eventLink;
    next.title = model.eventTitle;
    
    /* 取出model */
    [self.navigationController pushViewController:next animated:YES];
}







#pragma mark - Memory
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
